package com.yoomiito.app.present.goods;

import android.text.TextUtils;
import android.widget.Toast;

import com.yoomiito.app.base.AppConfig;
import com.yoomiito.app.model.bean.GoodsDetail;
import com.yoomiito.app.model.order.ShopCarList;
import com.yoomiito.app.net.YmApi;
import com.yoomiito.app.base.BaseActPresenter;
import com.yoomiito.app.ui.goods.GoodsInfoActivity;
import com.yoomiito.app.utils.AllCode;
import com.yoomiito.app.utils.EventMessage;
import com.yoomiito.app.utils.NetworkUtils;
import com.yoomiito.app.utils.YMToast;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import cn.droidlover.xdroidmvp.net.ApiException;
import cn.droidlover.xdroidmvp.net.ApiSubscriber;
import cn.droidlover.xdroidmvp.net.BaseModel;
import cn.droidlover.xdroidmvp.net.XApi;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class GoodsInfoPresenter extends BaseActPresenter<GoodsInfoActivity> {

    public GoodsInfoPresenter(AppConfig appConfig) {
        super(appConfig);
    }

    /**
     * 商品详情
     *
     * @param id
     */
    public void getGoodsInfo(String id, String type) {
        if (NetworkUtils.checkNetPermission(getV())) {
            return;
        }
        YmApi.getYmService()
                .getGoodsInfo(id, type)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<GoodsDetail>getApiTransformerData())
                .compose(XApi.<GoodsDetail>getScheduler())
                .subscribe(new ApiSubscriber<GoodsDetail>() {
                    @Override
                    public void onSuccess(GoodsDetail detail) {
                        getV().setGoodsInfo(detail);
//                        getV().dismissLoading();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
//                        if (300==error.getCode()){//商品下架
                        getV().pullMarket(error.getMsg());
//                        }
                    }
                });
    }

    /**
     * 关联商品
     *
     * @param id
     */
    public void getRelatedGoods(String id) {
        if (NetworkUtils.checkNetPermission(getV())) {
            return;
        }
        YmApi.getYmService()
                .getRelatedGoods(id)
                .compose(getV().bindToLifecycle())
                .compose(XApi.<List<GoodsDetail>>getApiTransformerData())
                .compose(XApi.<List<GoodsDetail>>getScheduler())
                .subscribe(new ApiSubscriber<List<GoodsDetail>>(handleException()) {
                    @Override
                    public void onSuccess(List<GoodsDetail> goodsDetails) {
                        getV().setRecommendGoods(goodsDetails);
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

    /**
     * 添加收藏
     *
     * @param id
     */
    public void addCollection(String id) {
        if (NetworkUtils.checkNetPermission(getV())) {
            return;
        }
        YmApi.getYmService()
                .addCollection(id)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>(handleException()) {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        getV().showToast("收藏成功");
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

    /**
     * 取消收藏
     *
     * @param id
     */
    public void delCollection(String id) {
        if (NetworkUtils.checkNetPermission(getV())) {
            return;
        }
        YmApi.getYmService()
                .delCollection(id)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>(handleException()) {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        getV().showToast("已取消收藏");
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

    //0元购商详
    public void getFreeGoodsInfo(String id, String productId) {
        YmApi.getYmService()
                .getFreeGoodsInfo(id, productId)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<GoodsDetail>(handleException()) {
                    @Override
                    public void onSuccess(GoodsDetail goodsDetail) {
                        getV().setGoodsInfo(goodsDetail);
//                        getV().dismissLoading();
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
//                        if (ApiException.GOODS_OBTAINED==error.getCode()){//商品下架
                        getV().pullMarket("");
//                        }
                    }
                });
    }

    //领取免费商品
    public void buyFreeGoods(String id, String productId) {
        YmApi.getYmServiceNoErrorCode().buyFreeGoods(id, productId)
                .compose(getV().bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(freeGoodsBuyInfoBaseModel -> {
                    getV().dismissLoading();
                    if (freeGoodsBuyInfoBaseModel.getCode() == 200) {
                        getV().buyFreeGoods(freeGoodsBuyInfoBaseModel.data.getSend_url());
                    } else if (freeGoodsBuyInfoBaseModel.getCode() == 5002) {
                        getV().showFreeGoodsCouponDialog(freeGoodsBuyInfoBaseModel.getData());
                    } else {
                        YMToast.showText(freeGoodsBuyInfoBaseModel.msg);
                    }
                });

    }

    public void addShopCar(String productId) {
        YmApi.getYmService()
                .addShopCar(productId, 1)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerNoData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<BaseModel>() {
                    @Override
                    public void onSuccess(BaseModel baseModel) {
                        EventMessage eventMessage = new EventMessage();
                        eventMessage.setEventType(AllCode.NotifyShopCarData);
                        EventBus.getDefault().postSticky(eventMessage);
                        getV().dismissLoading();
                        getV().showNewFlag(true);
                        YMToast.showText("添加成功", Toast.LENGTH_SHORT);
                    }

                    @Override
                    public void onFail(ApiException error) {
                        getV().dismissLoading();
                        if (!TextUtils.isEmpty(error.getMsg()))
                            YMToast.showText(error.getMsg());
                    }
                });

    }

    public void getList(int page){
        YmApi.getYmService()
                .showCarList(page,10)
                .compose(getV().bindToLifecycle())
                .compose(XApi.getApiTransformerData())
                .compose(XApi.getScheduler())
                .subscribe(new ApiSubscriber<ShopCarList>(handleException()) {
                    @Override
                    public void onSuccess(ShopCarList shopCarList) {
                        if (shopCarList==null||shopCarList.getData()==null||shopCarList.getData().size()==0){
                            getV().showNewFlag(false);
                        }else {
                            getV().showNewFlag(true);
                        }
                    }

                    @Override
                    public void onFail(ApiException error) {

                    }
                });
    }

}
